//
//  DOWebViewController.m
//  DooiooOnline
//
//  Created by koki on 14-12-5.
//  Copyright (c) 2014年 dooioo. All rights reserved.
//

#import "HDWebViewController.h"

@interface HDWebViewController ()
@end

@implementation HDWebViewController
@synthesize contentWebView,hidenWebNav,urlStr,titleStr,tipView;

-(void)dealloc
{
    urlStr=nil;
    titleStr=nil;
    contentWebView=nil;
    tipView = nil;
}


-(void)initNavigationBar
{
    [super initNavigationBar];
    leftBarBtn.hidden=self.hidenNavBtn;
    [leftBarBtn setImage:[UIImage imageNamed:@"navBarBack.png"] forState:UIControlStateNormal];
    
    self.navigationItem.title=self.titleStr;
}

- (void)setTitleStr:(NSString *)value
{
    titleStr = value;
    self.navigationItem.title = self.titleStr;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSURL* url=[NSURL URLWithString:self.urlStr relativeToURL:[NSURL URLWithString:@"http://"]];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [contentWebView loadRequest:request];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)initUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    contentWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 64, APPFRAME.size.width, APPFRAME.size.height-44)];
    contentWebView.scalesPageToFit =YES;
    contentWebView.delegate =(id)self;
    [contentWebView setBackgroundColor:[UIColor clearColor]];
    [contentWebView setOpaque:NO];
    if (hidenWebNav)
    {
        contentWebView.scrollView.bounces=NO;
        CGRect rect=CGRectMake(0, 64, APPFRAME.size.width, APPFRAME.size.height-44);
        contentWebView.frame=CGRectMake(0, -45, rect.size.width, rect.size.height+45);
    }
    [self.view addSubview:contentWebView];
    [self showTipsView];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    tipView.hidden = YES;
    [SXProgressHUD showHUDAddedTo:webView animated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    tipView.hidden = YES;
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if (!self.titleStr.length) {
        titleLable.text = title;
    }
    [SXProgressHUD hideAllHUDsForView:webView animated:YES];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [SXProgressHUD hideAllHUDsForView:webView animated:YES];
//    [netEngine showErrorAlertWithMsg:@"请求失败，请稍后再试"];
    tipView.hidden = NO;
}

//添加无数据时的提示界面
-(void)showTipsView
{
    CGRect rect=CGRectMake(0, 64, APPFRAME.size.width, APPFRAME.size.height-44);
    self.tipView=[[UIView alloc]initWithFrame:rect];
    tipView.backgroundColor=[UIColor clearColor];
    
    CGPoint point=tipView.center;
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];
    NSAttributedString* attrTip=[[NSAttributedString alloc]initWithString:@"无法打开此页面" attributes:
                                 @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                   NSParagraphStyleAttributeName:paragraphStyle,
                                   NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    
    NSString *remarkString =@"\n可能因为网页暂时出现故障或设备\n尚未接入网络，请检查后重试。";
    NSAttributedString* attrRemark=[[NSAttributedString alloc]initWithString:remarkString attributes:
                                    @{NSFontAttributeName:[UIFont systemFontOfSize:13],
                                      NSParagraphStyleAttributeName:paragraphStyle,
                                      NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    
    NSMutableAttributedString* attributeStr=[[NSMutableAttributedString alloc] initWithAttributedString:attrTip];
    [attributeStr appendAttributedString:attrRemark];
    
    UILabel* label=[HDUtil UILabelWithText:@"" fontSize:16];
    label.attributedText=attributeStr;
    label.frame=CGRectMake(0,0,tipView.frame.size.width, 100);
    label.textAlignment=NSTextAlignmentCenter;
    label.backgroundColor=[UIColor clearColor];
    label.numberOfLines=0;
    label.textColor=[UIColor darkGrayColor];
    label.center=CGPointMake(point.x, point.y-70);
    [tipView addSubview:label];
    [self.view addSubview:tipView];
    tipView.hidden=YES;
}

-(void)leftBarBtnAction
{
    [rootNavigation popViewControllerAnimated:YES];
}

-(void)rightBarBtnAction
{
//    [[NSNotificationCenter defaultCenter] postNotificationName:DOPopViewCloseNotification object:nil];
}

@end
